package com.yhny.lykj.yuhannongye.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.longsichao.lscframe.ctrl.DataManager;
import com.longsichao.lscframe.model.LSCModel;
import com.yhny.lykj.yuhannongye.R;
import com.yhny.lykj.yuhannongye.app.BaseActivity;
import com.yhny.lykj.yuhannongye.model.AboutModel;
import com.yhny.lykj.yuhannongye.net.NetHelper;
import com.yhny.lykj.yuhannongye.tools.Tools;

public class AboutActivity extends BaseActivity {
    private WebView webView;
    private int fromType;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
        fromType = getIntent().getIntExtra(FROM_TYPE_ID,0);
        TextView topName = (TextView)findViewById(R.id.name_top_bar);

        findViewById(R.id.left_group).setOnClickListener(this);
        findViewById(R.id.left_text).setOnClickListener(this);

        webView = (WebView) findViewById(R.id.web);
//        webView.getSettings().setAllowFileAccess(false);
//        webView.getSettings().setDefaultTextEncodingName("UTF-8");
//        webView.getSettings().setDisplayZoomControls(false);
//        webView.getSettings().setJavaScriptEnabled(false);
//        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient());
        webView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });

        showProgressDialog(R.string.text_loading);
//        if (fromType==FROM_TYPE_VIEW_REGISTER){
//            topName.setText("用户注册协议");
//            DataManager.getData(MODEL_PROCESS_0, NetHelper.User.getRegAgreement(), this);
//        }else{
            topName.setText("关于我们");
            DataManager.getData(MODEL_PROCESS_0, NetHelper.User.aboutUs(), this);
//        }

    }

    @Override
    public boolean onModel(int caseId, LSCModel model) {
        if (super.onModel(caseId, model)) {
            dismissProgressDialog();
            return false;
        }
        switch (caseId){
            case MODEL_PROCESS_0:{
                AboutModel aboutModel = (AboutModel)model;


                if (aboutModel!=null&& !TextUtils.isEmpty(aboutModel.mes)){
                    String context = aboutModel.mes.replace("\\","");
                    showData(context);
//                    webView.loadDataWithBaseURL("about:blank", aboutModel.data.content, "text/html", "utf-8", null);
                }else{
                    showData("");
                }
                break;
            }
        }
        dismissProgressDialog();
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.left_text:
            case R.id.left_group:{
                finish();
                break;
            }
        }
    }

    // Web视图
    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
    private void showData(String data) {
        if (data == null || data.isEmpty()) {
            data = getString(R.string.text_no_data);
        }
        webView.loadDataWithBaseURL("", Tools.HtmlToString(data), "text/html", "UTF-8", "");
    }
    public boolean onKeyDown(int keyCoder,KeyEvent event){
        if(webView.canGoBack() && keyCoder == KeyEvent.KEYCODE_BACK){
            webView.goBack();   //goBack()表示返回webView的上一页面
            return true;
        }else{
            finish();
        }
        return false;
    }
    @Override
    protected void onDestroy() {
        webView.destroy();
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        webView.onPause();
        super.onPause();
    }

    @Override
    protected void onResume() {
        webView.onResume();
        super.onResume();
    }
}
